<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\User;
use app\admin\model\Annual;
use app\admin\model\Data as DataModel;
use app\admin\model\Match as MatchModel;
use app\admin\model\DataClass;
use app\admin\model\DataClasses;
use app\admin\model\DataProf;
use app\admin\model\SportRecord;
use app\admin\model\TaskscoreRecord;
use app\admin\service\GradeService;
use app\admin\service\DepartmentsService;
use think\Db;


/**
 * H5-控制器
 * @author 牧羊人
 * @since 2020/11/19
 * Class WechatminiLogin
 * @package app\admin\controller
 */
class H5 extends Backend
{
    /**
     * 初始化
     * @author 牧羊人
     * @since 2020/11/19
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new TaskscoreRecord();
    }

    //列表
    public function list()
    {
        $user = new User();
        $uid = $this->userId;
        $param = request()->param();
        $userInfo = $user->getInfo($uid);
        if ($userInfo['ish5']!=1&&$userInfo['user_type']==2) {
            return $this->jsonReturn(message("权限不足", false));
        }
        $page = isset($param['page'])&&is_numeric($param['page']) ? $param['page'] : 1;
        $pageSize = isset($param['pageSize'])&&is_numeric($param['pageSize']) ? $param['pageSize'] : 10;
        //$sort = isset($param['sort']) && $param['order'] ? $param['sort'] . ' ' . $param['order'] : 'id desc';
        $sort = 'createdDate desc';
        // 参数

        // 查询条件
        $map = [];

        if ($param) {
            if (isset($param['sort']) && $param['sort']) {
                $sort = $param['sort']." ".$param["order"];
            }
            // 筛选名称
            if (isset($param['keywords']) && $param['keywords']) {
                $map[] = ['testerName|testerNumber', 'like', "%{$param['keywords']}%"];
            }
            // 筛选名称
            if (isset($param['username']) && $param['username']) {
                $map[] = ['testerName', 'like', "%{$param['username']}%"];
            }

            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $map[] = ['title', 'like', "%{$param['title']}%"];
            }

            // 项目
            if (isset($param['projectId']) && $param['projectId']) {
                $map[] = ['projectId', '=', $param['projectId']];
            }

            if (isset($param['type']) && $param['type']) {
                $map[] = ['type', '=', $param['type']];
            }

            // 学年
            if (isset($param['gradeId']) && $param['gradeId']) {
                $map[] = ['gradeId', '=', $param['gradeId']];
            }

            // 院系
            if (isset($param['departmentsId']) && $param['departmentsId']) {
                $map[] = ['departmentsId', '=', $param['departmentsId']];
            }

            // 专业
            if (isset($param['professionalId']) && $param['professionalId']) {
                $map[] = ['professionalId', '=', $param['professionalId']];
            }

            // 班级
            if (isset($param['classId']) && $param['classId']) {
                $map[] = ['classId', '=', $param['classId']];
            }

            // 性别
            if (isset($param['gender']) && $param['gender']) {
                $map[] = ['gender', '=', $param['gender']];
            }

            
            // 时间
            if (isset($param['start_end']) && $param['start_end']) {
                $start_end = $param['start_end'];
                $starttime = isset($start_end[0]) ? $start_end[0].' 00:00:00' : '';
                $endtime = isset($start_end[1]) ? $start_end[1].' 23:59:59' : '';
                // halt();
                if ($starttime&&$endtime) {
                    $map[] = ['createdDate', '>=', $starttime];
                    $map[] = ['createdDate', '<=', $endtime];
                }
            }
            
        }
        // $map[] = ['memberId', '<>', ''];
        $result = $this->model->where($map)->order($sort)->page(PAGE, PERPAGE)->column("id");
        $list = [];
        if (is_array($result)) {
            foreach ($result as $val) {
                $info = $this->model->getInfo2($val);
                $list[] = $info;
            }
        }

        $count = $this->model->where($map)->count();
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $this->jsonReturn($message);
    }

    /**
     * 删除同步记录数据
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function deleter()
    {
        $user = new User();
        $uid = $this->userId;
        $userInfo = $user->getInfo($uid);
        if ($userInfo['ish5']!=1&&$userInfo['user_type']==2) {
            return $this->jsonReturn(message("权限不足", false));
        }
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return $this->jsonReturn(message("记录ID不能为空", false));
        }
        if (is_array($ids)) {
            foreach ($ids as $val) {
                $sportrecord = SportRecord::where('tbid',$val)->find();
                $tbid = Db::name('sportrecord_del')->where(['tbid'=>$val])->value('tbid');
                if (!$tbid) {
                    $data = [
                        'tbid'=>$val,
                        'memberId'=>$sportrecord['memberId'],
                        'schoolId'=>$sportrecord['schoolId'],
                        'gradeId'=>$sportrecord['gradeId'],
                        'classId'=>$sportrecord['classId'],
                        'departmentsId'=>$sportrecord['departmentsId'],
                        'tiyu_superior'=>$sportrecord['tiyu_superior'],
                        'professionalId'=>$sportrecord['professionalId'],
                        'tiyu_professional'=>$sportrecord['tiyu_professional'],
                        'projectId'=>$sportrecord['projectId'],
                        'tiyu_class'=>$sportrecord['tiyu_class'],
                        'tiyu_class_string'=>$sportrecord['tiyu_class_string'],
                        'taskId'=>$sportrecord['taskId'],
                        'testerName'=>$sportrecord['testerName'],
                        'testerNumber'=>$sportrecord['testerNumber'],
                        'idCard'=>$sportrecord['idCard'],
                        'gender'=>$sportrecord['gender'],
                        'school'=>$sportrecord['school'],
                        'grade'=>$sportrecord['grade'],
                        'clazz'=>$sportrecord['clazz'],
                        'itemCode'=>$sportrecord['itemCode'],
                        'score'=>$sportrecord['score'],
                        'unit'=>$sportrecord['unit'],
                        'duration'=>$sportrecord['duration'],
                        'pingscore'=>$sportrecord['pingscore'],
                        'rating'=>$sportrecord['rating'],
                        'bmi'=>$sportrecord['bmi'],
                        'createdDate'=>$sportrecord['createdDate'],
                        'deletetime'=>date('Y-m-d H:i:s'),
                        'deleteuser'=>$uid
                    ];
                    Db::name('sportrecord_del')->insert($data);
                }
                
            }
            // 批量删除
            $result = $this->model->deleteDAll($ids,true);
            if (!$result) {

                return $this->jsonReturn(message("删除失败", false));
            }

            return $this->jsonReturn( message("删除成功"));
        } else {
            // 单个删除
            $info = $this->model->getInfo($ids);
            if ($info) {
                $sportrecord = SportRecord::where('tbid',$ids)->find();
                $tbid = Db::name('sportrecord_del')->where(['tbid'=>$ids])->value('tbid');
                if (!$tbid) {
                    $data = [
                        'tbid'=>$ids,
                        'memberId'=>$sportrecord['memberId'],
                        'schoolId'=>$sportrecord['schoolId'],
                        'gradeId'=>$sportrecord['gradeId'],
                        'classId'=>$sportrecord['classId'],
                        'departmentsId'=>$sportrecord['departmentsId'],
                        'tiyu_superior'=>$sportrecord['tiyu_superior'],
                        'professionalId'=>$sportrecord['professionalId'],
                        'tiyu_professional'=>$sportrecord['tiyu_professional'],
                        'projectId'=>$sportrecord['projectId'],
                        'tiyu_class'=>$sportrecord['tiyu_class'],
                        'tiyu_class_string'=>$sportrecord['tiyu_class_string'],
                        'taskId'=>$sportrecord['taskId'],
                        'testerName'=>$sportrecord['testerName'],
                        'testerNumber'=>$sportrecord['testerNumber'],
                        'idCard'=>$sportrecord['idCard'],
                        'gender'=>$sportrecord['gender'],
                        'school'=>$sportrecord['school'],
                        'grade'=>$sportrecord['grade'],
                        'clazz'=>$sportrecord['clazz'],
                        'itemCode'=>$sportrecord['itemCode'],
                        'score'=>$sportrecord['score'],
                        'unit'=>$sportrecord['unit'],
                        'duration'=>$sportrecord['duration'],
                        'pingscore'=>$sportrecord['pingscore'],
                        'rating'=>$sportrecord['rating'],
                        'bmi'=>$sportrecord['bmi'],
                        'createdDate'=>$sportrecord['createdDate'],
                        'deletetime'=>date('Y-m-d H:i:s'),
                        'deleteuser'=>$uid
                    ];
                    Db::name('sportrecord_del')->insert($data);
                }
                $result = $this->model->doDelete(['id'=>$ids]);
                if (!$result) {
                    return $this->jsonReturn(message("删除失败", false));
                }
                    return $this->jsonReturn(message());
                
            }
            return $this->jsonReturn(message($this->model->getError(), false));
        }
        
    }

}